package edu.tongji.jiaowu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tongji.quickCRUDQ.service.impl.BaseManagerImpl;
import com.tongji.quickCRUDQ.util.StringUtil;

import edu.tongji.jiaowu.dao.ExternalExamStudentsDao;
import edu.tongji.jiaowu.dao.ExternalExamTypeDao;
import edu.tongji.jiaowu.service.ExternalExamTypeManager;
import edu.tongji.jiaowu.service.ParameterManager;

import com.tongji.quickCRUDQ.model.CommonBean;

public class ExternalExamTypeManagerImpl extends BaseManagerImpl implements ExternalExamTypeManager {
	private ExternalExamTypeDao externalExamTypeDao;
	private ExternalExamStudentsDao externalExamStudentsDao;
	private ParameterManager parameterManager;

	public CommonBean createExternalExamType() {
		return externalExamTypeDao.createExternalExamType();
	}

	/*
	 public CommonBean findExternalExamTypes(String refid) {
	 return externalExamTypeDao.findExternalExamTypes(refid);
	 }
	 */
	public CommonBean findExternalExamTypeById(String id) {
		return externalExamTypeDao.findExternalExamTypeById(id);
	}
	
	public CommonBean findExternalExamTypeByCode(String currentYear, String currentSemester, String currentType) {
		return externalExamTypeDao.findExternalExamTypeByCode(currentYear, currentSemester, currentType);
	}
	

	public CommonBean findAllExternalExamTypes() {
		return externalExamTypeDao.findAllExternalExamTypes();
	}

	public void insertExternalExamType(CommonBean externalExamTypeBean) {
		externalExamTypeDao.insertExternalExamType(externalExamTypeBean);
	}
	
	public void reset(CommonBean externalExamTypeBean) {
		externalExamTypeDao.remove(externalExamTypeBean);
		externalExamTypeDao.insertExternalExamType(externalExamTypeBean);
	}
	public void updateExternalExamType(CommonBean externalExamTypeBean) {
		externalExamTypeDao.updateExternalExamType(externalExamTypeBean);
	}

	public CommonBean findPermitTypes(String currentYear, String currentSemester, String studentNo) {
		CommonBean entryTypes=externalExamTypeDao.findEntryTypes(currentYear,currentSemester);//所有“正在报名”的考试类型
		CommonBean types=externalExamStudentsDao.findTypes(currentYear,currentSemester,studentNo);//已报名类型
		int num1=types.getSize();
		for (int i = 0; i < num1; i++) {
			String examType=types.getPropertyAsString(i, "examType");
			CommonBean types2=entryTypes.query("examType", examType);
			entryTypes.remove("examType", examType);//已报名类型不能再次报名

			String conflictTypeStr= types2.getPropertyAsString(0, "conflictType");//冲突类型字符串
			if (conflictTypeStr==null||"".equals(conflictTypeStr)) {//没有与已报名类型冲突的考试类型
				continue;
			}
			String[] conflictTypes=StringUtil.splitStr(conflictTypeStr, ",");
			for (int j = 0; j < conflictTypes.length; j++) {
				entryTypes.remove("examType", conflictTypes[j]);//与已报名类型冲突的考试类型不能报名
			}
		}
		
		return entryTypes;
	}
	

	public void updateSwitch(String entrySwitch, String year, String semester) {
		Map map=new HashMap();
		map.put("EXTERNAL_EXAM_ENTRY_SWITCH", entrySwitch);
		parameterManager.updateParameter(map);
		if ("N".equals(entrySwitch)) {
			externalExamTypeDao.closeAllTypes(year,semester);
		}
	}

	public ExternalExamTypeDao getExternalExamTypeDao() {
		return externalExamTypeDao;
	}
	
	public void setExternalExamTypeDao(ExternalExamTypeDao externalExamTypeDao) {
		this.externalExamTypeDao = externalExamTypeDao;
	}

	public ExternalExamStudentsDao getExternalExamStudentsDao() {
		return externalExamStudentsDao;
	}

	public void setExternalExamStudentsDao(ExternalExamStudentsDao externalExamStudentsDao) {
		this.externalExamStudentsDao = externalExamStudentsDao;
	}


	public ParameterManager getParameterManager() {
		return parameterManager;
	}

	public void setParameterManager(ParameterManager parameterManager) {
		this.parameterManager = parameterManager;
	}



}
